//-----------------------------------------------------------------------
// <copyright file="CountryDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CountryDAOBase
    {
        #region Common methods
        public virtual Country CreateCountryFromReader(IDataReader reader)
        {
            Country item = new Country();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CountryId"))) item.CountryId = (long)reader["CountryId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("Img"))) item.Img = (string)reader["Img"];
                if (!reader.IsDBNull(reader.GetOrdinal("Other"))) item.Other = (string)reader["Other"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCountryFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCountry methods
            
        public virtual void CreateCountry(Country country)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCountryInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, country.Name);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, country.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, country.Enable);
                database.AddInParameter(dbCommand, "@Img", DbType.String, country.Img);
                database.AddInParameter(dbCommand, "@Other", DbType.String, country.Other);
                database.AddOutParameter(dbCommand, "@CountryId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                country.CountryId = (long)database.GetParameterValue(dbCommand, "@CountryId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCountryException, ex);
            }
        }

        #endregion

        #region UpdateCountry methods
        
        public virtual void UpdateCountry(Country country)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCountryUpdate");            
                
                database.AddInParameter(dbCommand, "@CountryId", DbType.Int64, country.CountryId);
                database.AddInParameter(dbCommand, "@Name", DbType.String, country.Name);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, country.Order);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, country.Enable);
                database.AddInParameter(dbCommand, "@Img", DbType.String, country.Img);
                database.AddInParameter(dbCommand, "@Other", DbType.String, country.Other);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCountryException, ex);
            }
        }
        
        #endregion

        #region DeleteCountry methods
        public virtual void DeleteCountry(long countryId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCountryDelete");
                
                database.AddInParameter(dbCommand, "@CountryId", DbType.Int64, countryId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCountryException, ex);
            }
        }

        #endregion

        #region GetCountry methods
        
        public virtual Country GetCountry(long countryId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCountryGet");
                
                database.AddInParameter(dbCommand, "@CountryId", DbType.Int64, countryId);
                
                Country country =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        country = CreateCountryFromReader(reader);
                        reader.Close();
                    }
                }
                return country;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCountryException, ex);
            }
        }
        
        #endregion

        #region GetCountryList methods
        public virtual CountryCollection GetCountryList(CountryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCountryGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CountryCollection countryCollection = new CountryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Country country = CreateCountryFromReader(reader);
                        countryCollection.Add(country);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return countryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCountryListException, ex);
            }
        }
        
        public virtual CountryCollection GetCountryList(CountryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCountryList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCountryList methods
        public virtual CountryCollection GetCountryListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCountryGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CountryCollection countryCollection = new CountryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Country country = CreateCountryFromReader(reader);
                        countryCollection.Add(country);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return countryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCountryListException, ex);
            }
        }
        
        public virtual CountryCollection GetCountryListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCountryListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}